/**
 * 
 */
package com.manthano.util.web.converter;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Currency;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.manthano.util.web.service.ServiceResponseConverter;
import com.manthano.util.web.service.bo.ServiceResponse;

/**
 * @author DELL
 */
public class JsonResponseConverter implements ServiceResponseConverter
{
	private Gson	gson	= null;

	/**
	 * 
	 */
	public JsonResponseConverter()
	{
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder = gsonBuilder.serializeNulls();
		gsonBuilder = gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
		gsonBuilder = gsonBuilder.registerTypeAdapter(Currency.class, new CurrencySerializer());
		gsonBuilder = gsonBuilder.registerTypeAdapter(Timestamp.class, new DateSerializer());
		gson = gsonBuilder.create();
	}

	@Override
	public String getResponseString(ServiceResponse serviceResponse)
	{
		return gson.toJson(serviceResponse);
	}

	@Override
	public String getResponseString(List<ServiceResponse> serviceResponses)
	{
		return gson.toJson(serviceResponses);
	}

	@Override
	public void setResponseHeader(HttpServletResponse response)
	{
		response.setHeader("Content-type", "text/x-json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.setHeader("Pragma", "no-cache");
	}

	private static class CurrencySerializer implements InstanceCreator<Currency>, JsonSerializer<Currency>,
			JsonDeserializer<Currency>
	{
		public Currency createInstance(Type type)
		{
			return Currency.getInstance("en-us");
		}

		public JsonElement serialize(Currency currency, Type type, JsonSerializationContext context)
		{
			return new JsonPrimitive(currency.getCurrencyCode());
		}

		public Currency deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
				throws JsonParseException
		{
			return Currency.getInstance(jsonElement.getAsString());
		}
	}

	private static class DateSerializer implements InstanceCreator<Date>, JsonSerializer<Date>, JsonDeserializer<Date>
	{

		public Date createInstance(Type type)
		{
			return new Date();
		}

		public JsonElement serialize(Date date, Type type, JsonSerializationContext context)
		{
			return new JsonPrimitive(date.getTime());
		}

		public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
				throws JsonParseException
		{
			return new Date(jsonElement.getAsLong());
		}

	}
}